package com.ruanyun.sys.modular.system.service.impl;

import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.sys.modular.system.entity.UserSta;
import com.ruanyun.sys.modular.system.mapper.UserStaMapper;
import com.ruanyun.sys.modular.system.model.params.UserStaParam;
import com.ruanyun.sys.modular.system.model.result.UserStaResult;
import  com.ruanyun.sys.modular.system.service.UserStaService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 用户岗位表 服务实现类
 * </p>
 *
 * @author wangchuan
 * @since 2019-08-10
 */
@Service
public class UserStaServiceImpl extends ServiceImpl<UserStaMapper, UserSta> implements UserStaService {

    @Override
    public void add(UserStaParam param){
        UserSta entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(UserStaParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserStaParam param){
        UserSta oldEntity = getOldEntity(param);
        UserSta newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserStaResult findBySpec(UserStaParam param){
        return null;
    }

    @Override
    public List<UserStaResult> findListBySpec(UserStaParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserStaParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(UserStaParam param){
        return param.getUserStaId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserSta getOldEntity(UserStaParam param) {
        return this.getById(getKey(param));
    }

    private UserSta getEntity(UserStaParam param) {
        UserSta entity = new UserSta();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
